package com.huluobo.day_1_lifecycledemo.livedata;

import androidx.arch.core.util.Function;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Transformations;
import androidx.lifecycle.ViewModel;

/**
 * LiveData总结:
 * 1.LiveData是一个可被观测的对象(被观察者),一般通过Observer来进行观测,当它发生改变时,会触发onChange回调
 * 2.LiveData,ViewModel,和Lifecycle是联用的
 * 3.map是LiveData转换的方法,通过观测LiveData中的属性,可以获取一个LiveData对象
 */
public class StudentViewModel extends ViewModel {
    private MutableLiveData<Student> studentMutableLiveData = new MutableLiveData<>();

    public LiveData<Integer> getStuScore() {
        return Transformations.map(studentMutableLiveData, new Function<Student, Integer>() {
            @Override
            public Integer apply(Student input) {
                return input.getScore();
            }
        });
    }

    private LiveData<Integer> stuScore;

    public LiveData<Student> getStudentMutableLiveData() {
        return studentMutableLiveData;
    }

    public void setStudentMutableLiveData(Student student) {
        studentMutableLiveData.setValue(student);
    }
}
